import { useEffect, useState, useMemo } from 'react';

export const useGetProcessNumber = (dynamicdata: any[], userInfo: { ID: unknown; }, isChange: unknown, rangeConfig: string, processGroup: { selectedRows: string | any[]; groupGuid: string;}, getProcessCenterNumber: any) => {
  const [mydynamicdata, $mydynamicdata] = useState(dynamicdata || []);
  const [data, setData] = useState(null);
  const countEnum: any = useMemo(() => {
    return {
      '1': 'ApplyCount',
      '2': 'InboxCount',
      '3': 'inApplication',
      '4': 'drafts',
      '5': 'CctomeCount',
      '6': 'InitCount',
      '7': 'CompleteCount',
      '8': 'ArchiveCount',
    }
  }, []);

  const typesEnum: any = useMemo(() => {
    return {
      '1': 'apply',
      '2': 'inbox',
      '3': 'applying',
      '4': 'drafts',
      '5': 'cctome',
      '6': 'init',
      '7': 'complete',
      '8': 'archive',
    }
  }, []);

  useEffect(() => {
    if (userInfo?.ID) {
      const params = {
        userId: userInfo?.ID,
        dptKey: (rangeConfig == 'all' || processGroup?.selectedRows?.length == 0) ? '' : processGroup?.groupGuid || '',
        type: dynamicdata?.map((item) => typesEnum[item?.id])?.join(','),
      };
      getProcessCenterNumber(params).then((res) => {
        let _data = res?.details;
        setData(_data)
      });
    }
  }, [userInfo?.ID, isChange, rangeConfig, dynamicdata]);

  useEffect(() => {
    if (data) {
      let _mydynamicdata = dynamicdata?.map((item: any) => {
        return {
          ...item,
          count: data?.[countEnum?.[item?.id]]
        }
      });
      $mydynamicdata(_mydynamicdata);
    }
  }, [dynamicdata, data])

  return mydynamicdata;
}
